package com.weipai.demo.service.impl;

import com.weipai.demo.enums.RetCode;
import com.weipai.demo.mapper.base.JobNewsMapper;
import com.weipai.demo.model.JobNews;
import com.weipai.demo.service.NewsSaveService;
import com.weipai.demo.util.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: 赵项阳
 * @Date: 2019/6/14 10:38
 * @Version 1.0
 */
@Service
public class NewsSaveServicelmpl implements NewsSaveService {
    private final static Logger logger = LoggerFactory.getLogger(NewsSaveServicelmpl.class);

    @Autowired
    private JobNewsMapper jobNewsMapper;

    //兼职信息的添加或修改
    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Response save(JobNews jobNews) {
        Response response=new Response();
        try {
            String i=jobNews.getDetails();
            if (i.equals("")||i==null||i=="undefined"){
                response.setCode(RetCode.MESSAGE_NULL);
                return response;
            }
            if (jobNews.getId()!=null){
                jobNewsMapper.updateByPrimaryKey(jobNews);
            }else {
                String s=jobNews.getDetails();
                if (s.equals("")||s==null||s=="undefined"){
                    response.setCode(RetCode.MESSAGE_NULL);
                    return response;
                }
                jobNewsMapper.insert(jobNews);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("添加或者修改失败:"+e.getMessage());
        }
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    //兼职信息的删除
    @Override
    public Response deleteById(JobNews jobNews) {
        Response response=new Response();
        if (jobNews.getId()!=null){
            jobNewsMapper.delete(jobNews);
        }
        response.setCode(RetCode.DELETESUCCESS);
        return response;
    }
}
